<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion">后台管理</i>
      </el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>新增SPU</el-breadcrumb-item>
      <el-breadcrumb-item>第4步：填写商品详情</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="基本信息">
        <el-descriptions :title="ruleForm.name" border>
          <el-descriptions-item label="品牌">
            <el-tag size="small">{{ ruleForm.brandName }}</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="类别" span="2">{{ ruleForm.categoryName }}</el-descriptions-item>
          <el-descriptions-item label="标题" span="3">{{ ruleForm.title }}</el-descriptions-item>
          <el-descriptions-item label="简介" span="3">{{ ruleForm.description }}</el-descriptions-item>
          <el-descriptions-item label="相册" span="3">{{ ruleForm.albumName }}</el-descriptions-item>
          <el-descriptions-item label="货号">{{ ruleForm.typeNumber }}</el-descriptions-item>
          <el-descriptions-item label="售价">{{ ruleForm.listPrice }}</el-descriptions-item>
          <el-descriptions-item label="计件单位">{{ ruleForm.unit }}</el-descriptions-item>
          <el-descriptions-item label="库存">{{ ruleForm.stock }}</el-descriptions-item>
          <el-descriptions-item label="库存阈警值">{{ ruleForm.stockThreshold }}</el-descriptions-item>
          <el-descriptions-item label="排序序号">{{ ruleForm.sort }}</el-descriptions-item>
          <el-descriptions-item label="关键词">{{ ruleForm.keywords }}</el-descriptions-item>
          <el-descriptions-item label="标签">{{ ruleForm.tags }}</el-descriptions-item>
        </el-descriptions>
      </el-form-item>
      <el-form-item label="商品详情">
        <div id="editor"></div>
      </el-form-item>
      <el-form-item>
        <el-button type="warning" @click="goBack()">上一步</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具 js，第三方插件 js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import 引入的组件需要注入到对象中才能使用
  components: {},
  props: {},
  data() {
    //这里存放数据
    return {
      editor: {},
      ruleForm: {},
      rules: {},
    };
  },
  //计算属性 类似于 data 概念
  computed: {},
  //监控 data 中的数据变化
  watch: {},
  //方法集合
  methods: {
    initWangEditor() {
      this.editor = new this.E("#editor");
      this.editor.config.zIndex = 1;
      this.editor.create();
    },
    localRuleForm() {
      let localRuleFormString = localStorage.getItem("localRuleForm");
      if (!localRuleFormString) {
        this.$router.push("add-new-1");
      }
      let localRuleForm = JSON.parse(localRuleFormString);
      // console.log(localRuleForm);
      if (!localRuleForm.categoryId) {
        this.$router.push("add-new-1");
      } else if (!localRuleForm.brandId) {
        this.$router.push("add-new-2");
      } else if (!localRuleForm.albumId) {
        this.$router.push("add-new-3");
      }

      this.ruleForm = localRuleForm;
    },
    goBack() {
      this.$router.push("add-new-3");
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let detail = this.editor.txt.html();
          this.ruleForm.detail = detail;
          console.log("即将提交到服务器端的数据");
          console.log(this.ruleForm);
          let formData = this.qs.stringify(this.ruleForm);
          let url = "http://localhost:8088/spu/add-new";
          this.axios
            .create({ headers: { Authorization: localStorage.getItem("jwt") } })
            .post(url, formData)
            .then((response) => {
              let responseBody = response.data;
              if (responseBody.state == 20000) {
                // this.$message({
                //   showClose: true,
                //   message: "添加SPU成功！",
                //   type: "success",
                // });
                // this.$router.push("add-new-1");
                this.$alert("添加SPU成功！", "提示", {
                  confirmButtonText: "确定",
                  callback: (action) => {
                    this.$router.push("add-new-1");
                  },
                });
              } else {
                this.$message.error(responseBody.message);
              }
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
  //生命周期 - 创建完成（可以访问当前 this 实例）
  created() {},
  //生命周期 - 挂载完成（可以访问 DOM 元素）
  mounted() {
    this.localRuleForm();
    this.initWangEditor();
  },
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有 keep-alive 缓存功能，这个函数会触发
};
</script>
<style scoped></style>
